import { Toast } from "antd-mobile";
import axios from 'axios';

const baseURL = 'https://api-haoke-web.itheima.net';
// 创建自定义 axios 实例
const newAxios = axios.create({ baseURL });

let ajaxCount = 0;

newAxios.interceptors.request.use(function (config) {

  Toast.loading('加载中...', 0);

  ajaxCount++;

  return config;
}, function (error) {

  return Promise.reject(error);
});


newAxios.interceptors.response.use(function (response) {

  ajaxCount--;

  if (ajaxCount === 0) {

    Toast.hide();
  }

  return response.data.body;
}, function (error) {

  Toast.offline('网络出问题了.');
  return Promise.reject(error);
});


newAxios.baseURL = baseURL;

export default newAxios;


